<?php
require_once 'models/order.inc';
require_once 'models/orm.inc';


class TMSOrderPositionItem extends TCandyItem {	
	public function __get($nm){
		switch ($nm){
			case 'Price':return $this->Product->OrderPrice(false,$this);break;
			case 'Cost':return $this->OrderPrice*$this->Quantaty;break;
			case 'Discounts':return $this->Model->AppliedDiscounts($this);break;
			default:return parent::__get($nm);break;
		}
	}
	
	protected function checkProperties(){
		parent::checkProperties();
		$opts = $this->Product->OrderOptions($this);
		foreach ($opts as $nm=>$opt)
			$this->properties[$nm] = new TCandyItemProperty($this, $opt);
	}	
}


class TMSOrderPositionModel extends TCandyModel {
	
/**
 * @var TMSDiscountModel
 */	
	protected $_ioc_discounts_model_;
	
	protected function afterConstruct(){
		$this->_conf_classname_ = 'TMSOrderPosition';
		$this->_array_id_properties_ = array('Order','Product','DataHash');
	}
	
	private function assignOptions($options, $data){
		foreach ($data as $nm => $v)
			if ($nm != 'Order' && $nm != 'Product' && $nm != 'Data' && $nm != 'DataHash')
			$options[$nm] = $v;
	
		$data['Data'] = json_encode($options);
		return array_diff($data, $options);
	}
	
	protected function preCreateItem(array $data, $class = null){
		return $this->assignOptions(array(),$data);
	}
	
	protected function preEditItem($id,array $data, $origin){
		return $this->assignOptions($origin->Options(),$data);
	}	
	
	protected function beforeSave(TNOrmObject $object){
		if (!$object->Data)
			$object->DataHash = '';
		else 
			$object->DataHash = THash::Apply($object->Data,THash::MD5);
		return $object;
	}
	
	public function CheckItemProperty(TNOrmObject $object, TOrmPropertyMeta $property){
		if ($object->Order->State != TMSOrderState::INWORK)
			$property->ReadOnly = true;		
		return true;
	}
	
	protected function postCreateItem(TCandyItem $item, array $data = array()){
		$item->Product->Reserved = $item->Product->Reserved?$item->Product->Reserved:0 + $item->Quantaty;
		$item->Product->Save();
	}	
	
	protected function postEditItem(TCandyItem $item, $origin, array $data = array()){
		$item->Product->Reserved = $item->Product->Reserved - $origin->Quantaty + $item->Quantaty;
		$item->Product->Save();
	}
	
	public function DeleteItem($id){
		$item = $this->GetItem($id);
		$item->Product->Reserved = $item->Product->Reserved - $item->Quantaty;
		if ($item->Product->Reserved < 0)
			$item->Product->Reserved = 0;
		$item->Product->Save();
		parent::DeleteItem($id);
	}	
	protected function postProcessProperties(){
		parent::postProcessProperties();
		$this->propertyMetas['Order']->ReadOnly = true;
		$this->propertyMetas['Product']->ReadOnly = true;
		$this->propertyMetas['OrderPrice']->ReadOnly = true;		
		$this->propertyMetas['Cost'] = new TOrmPropertyMeta('Cost','Cost',TItemPropertyType::PT_DEC);
		$this->propertyMetas['Cost']->ReadOnly = true;
		$this->propertyMetas['ProductQuantaty'] = new TOrmPropertyMeta('ProductQuantaty','In stock',TItemPropertyType::PT_INT);
		$this->propertyMetas['ProductQuantaty']->ReadOnly = true;
		$this->propertyMetas['Discounts'] = new TOrmCollectionPropertyMeta('Discounts', 'Discounts', 'TMSDiscount');
		$this->propertyMetas['Discounts']->ReadOnly = true;
	}		
	
	public function AppliedDiscounts(IItem $orderpos){
		$ids = explode(',',$orderpos->AppliedDiscounts);
		$result = array();
		foreach ($ids as $id)
			$result[] = $this->DiscountsModel->GetItem($id);
		return $result;
	}
	
/**
 * @param object $object
 * @return TCandyItem
 */
	public function WrapObject($object, array &$caller_stack = array()){
		if ($object instanceof $this->_conf_classname_)
			return new TMSOrderPositionItem($object,$this);
		return parent::WrapObject($object,$caller_stack);
	}
	
}